import APIZPZ from "../../../utils/apiZpz";

// pages/home/home.ts
const TAG = <any>{
  1: '上次',
  2: '本机',
  3: '热门'
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchText: '',
    oldTime: 0,
    currentModelInfo: {
      id: '',
      newPrice: 0,
      modelName: '',
      tag: '',
      icon: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const systemInfo = wx.getSystemInfoSync();
    const name = systemInfo.model.match(/<(.*)>/)?.[0].replace(/<|>/g, '') || '';
    APIZPZ.getHomeModel(name).then((res: any) => {
      console.log(res)
      this.setData({
        ['currentModelInfo.id']: res.data.id,
        ['currentModelInfo.newPrice']: res.data.newPrice,
        ['currentModelInfo.modelName']: res.data.modelName,
        ['currentModelInfo.tag']: TAG[res.data.tag],
        ['currentModelInfo.icon']: res.data.icon,
      })
    })
  },
  toQuestion(){
    wx.navigateTo({ url: '/pages/tabBarPage/mine/help/help' })
  },
  toSearch() {
    wx.navigateTo({ url: '/pages/search/search' })
  },
  toCategory(e: any) {
    const data = e.currentTarget.dataset;
    wx.navigateTo({ url: `/pages/tabBarPage/category/category?activedId=${data.id}` })
  },
  toEvaluate() {
    const time = new Date().getTime();
    if (time - this.data.oldTime < 1000) {
      return;
    }
    this.data.oldTime = time;
    APIZPZ.getModelEvaluate(this.data.currentModelInfo.id).then(() => {
      wx.navigateTo({
        url: `/pages/evaluate/model-evaluate/model-evaluate?id=${this.data.currentModelInfo.id}`
      });
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().setData({
      selectedIndex: 0
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})